package io.jee.caisan.weixin.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import io.jee.alaska.util.IpUtils;
import io.jee.alaska.util.Result;
import io.jee.caisan.weixin.config.ConfigProperties;
import io.jee.caisan.weixin.config.SessionManager;
import io.jee.caisan.weixin.config.UrlManager;
import io.jee.caisan.weixin.domain.Member;
import io.jee.caisan.weixin.service.MemberService;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;

@Controller
@RequestMapping("/user")
public class UserController {
	
	@Resource
	private WxMpService wxService;
	
	@Resource
	private SessionManager sessionManager;
	
	@Resource
	private MemberService memberService;
	
	@Resource
	private ConfigProperties configProperties;
	
	@Resource
	private UrlManager urlManager;

	/**
	 * 登录绑定-页面
	 * @param model
	 * @param code 从微信得到，换取access_token
	 * @param state 从微信得到
	 * @return
	 * @throws WxErrorException 
	 */
	@GetMapping("/login")
	public String login(Model model, HttpServletRequest request) throws WxErrorException {
		String openid = sessionManager.getSessionOpenid(request);
		Member member = memberService.findByOpenid(openid);
		if(member != null) {
			return "redirect:" + UrlManager.index;
		}
		return "user/login";
	}
	
	/**
	 * 登录绑定保存
	 * @param model
	 * @param request
	 * @param username
	 * @param password
	 * @return
	 * @throws WxErrorException
	 */
	@PostMapping("/login")
	public @ResponseBody Result<?> postLogin(Model model, HttpServletRequest request, String username, String password) throws WxErrorException {
		String openid = sessionManager.getSessionOpenid(request);
		return memberService.binding(username, password, openid);
	}
	
	/**
	 * 忘记密码-页面
	 * @param model
	 * @return
	 */
	@GetMapping("/forgot")
	public String forgot(Model model) {
		return "user/forgot";
	}
	
	/**
	 * 忘记密码-保存
	 * @param model
	 * @param username
	 * @param password
	 * @param code
	 * @return
	 */
	@PostMapping("/forgot")
	public @ResponseBody Result<?> postForgot(Model model, String username, String password, String code) {
		return memberService.forgotPassword(username, password, code);
	}
	
	/**
	 * 注册-页面
	 * @param model
	 * @return
	 */
	@GetMapping("/register")
	public String register(Model model) {
		return "user/register";
	}
	
	/**
	 * 注册-保存
	 * @param model
	 * @param request
	 * @param username
	 * @param password
	 * @param code
	 * @return
	 * @throws WxErrorException 
	 */
	@PostMapping("/register")
	public @ResponseBody Result<?> postForgot(Model model, HttpServletRequest request, String username, String password, String code) throws WxErrorException {
		String regip = IpUtils.getIpAddr(request);
		String openid = sessionManager.getSessionOpenid(request);
		return memberService.register(openid, username, password, code, regip);
	}
	
	/**
	 * 个人中心-页面
	 * @param model
	 * @param request
	 * @return
	 */
	@GetMapping("/personal")
	public String personal(Model model, HttpServletRequest request) {
		Member member = sessionManager.getSessionMember(request);
		model.addAttribute("member", memberService.getMember(member.getMid()));
		return "user/personal";
	}
	
	/**
	 * 解绑
	 * @param request
	 * @return
	 */
	@PostMapping("/unbind")
	public @ResponseBody Result<?> unbind(HttpServletRequest request) {
		Member member = sessionManager.getSessionMember(request);
		Result<Void> result = memberService.unbind(member.getMid());
		if(result.isSuccess()) {
			sessionManager.setSessionMember(request, null);
		}
		return result;
	}
}
